package cn.gson.crmdemo.controller.sale;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Customer;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Saleslog;
import cn.gson.crmdemo.model.pojo.Salesopportunity;
import cn.gson.crmdemo.model.service.CustomerService;
import cn.gson.crmdemo.model.service.SalesService;
import cn.gson.crmdemo.model.vo.EmpVo;
import cn.gson.crmdemo.model.vo.ReturndetailedReturnProductVo;
import cn.gson.crmdemo.model.vo.SaleVo;
import cn.gson.crmdemo.model.vo.SaleslogVo;

@Controller
public class SaleController {
	@Autowired
	SalesService ss;
	@Autowired
	CustomerService cs;

	/**
	 * 销售机会查询所有
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("sales-All")
	@ResponseBody
	public Map<String, Object> selectSalesAll(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = ss.selectSalesopportunity();
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 销售机会模糊查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("sales-select")
	@ResponseBody
	public Map<String, Object> selectSales(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize, String saleTheme) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = ss.selectSaleTheme(saleTheme);
		System.err.println("=====================sal" + saleTheme);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 客户模糊查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("cus-select")
	@ResponseBody
	public Map<String, Object> selectMoHuCustomerName(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize, String customerName) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = cs.selectMoHuCustomerName(customerName);
		System.err.println("=====================cus" + customerName);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	@RequestMapping("cus-select2")
	@ResponseBody
	public JSONArray cusJson2(String customerName) {
		System.out.println("customerName");
		// 查客户id名字
		JSONArray list = cs.selectMoHuCustomerName(customerName);
		// 返回
		return list;
	}

	/**
	 * 客户查询所有
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("cus-All")
	@ResponseBody
	public Map<String, Object> selectCustomerAll(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = cs.selectCustomerAll();
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 选择查询客户
	 * 
	 * @return
	 */
	@RequestMapping("cus-json")
	@ResponseBody
	public Map<String, Object> cusJson(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = cs.selectCustomer();
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}
	
	/**
	 * 客户查id名字
	 * 
	 * @return
	 */

	@RequestMapping("sale-json")
	@ResponseBody
	public Map<String, Object> saleJson(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize,Integer customerId) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = ss.selectSaleName(customerId);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}
	
	/**
	 * 根据状态查询
	 * 
	 * @param saleState
	 * @return
	 */
	@RequestMapping("sale-State")
	@ResponseBody
	public Map<String, Object> saleState(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize, Integer saleState) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = ss.selectState(saleState);
		System.err.println("=====================saleState:" + saleState);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 根据状态查询
	 * 
	 * @param saleState
	 * @return
	 */
	@RequestMapping("cus-Type")
	@ResponseBody
	public Map<String, Object> cusState(@RequestParam("page") Integer pageNum,
			@RequestParam("pageSize") Integer pageSize, Integer cusType) {
		Page<Object> page = PageHelper.startPage(pageNum, pageSize);
		// 当前页的内容
		JSONArray list = cs.selectType(cusType);
		System.err.println("=====================CustomerType:" + cusType);
		// 总记录数
		long total = page.getTotal();
		Map<String, Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}


	
	
	/**
	 * 删除销售机会
	 * 
	 * @param saleId
	 * @return
	 */
	// 增删改之后，一定要重定向到查询的接口
	@RequestMapping("delete-sales")
	public String deleteSales(Integer saleId) {
		ss.deleteSales(saleId);
		return "redirect:sales-All?page=1&pageSize=7";
	}

	/**
	 * 删除一条客户数据
	 * 
	 * @param customerId
	 * @return
	 */
	// 增删改之后，一定要重定向到查询的接口
	@RequestMapping("delete-cust")
	public String deleteCus(Integer customerId) {
		System.err.println("+++++++customerId"+customerId);
		cs.deleteCustomer(customerId);
		return "redirect:cus-All?page=1&pageSize=7";
	}

	/**
	 * 修改销售机会
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("update-sales")
	public String updateSales(SaleVo sales) {
		ss.updateSales(sales);
		cs.updateCus2(sales.getCustomerId());
		return "redirect:sales-All?page=1&pageSize=7";
	}

	/**
	 * 修改客户
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("update-cus")
	public String updateCus(Customer customer) {
		System.err.println("===========id" + customer.getCustomerId());
		System.err.println("=============mz" + customer.getCustomerName());
		
		cs.updateCus(customer);
		cs.updateCus1(customer.getCustomerId());
		cs.updateCus2(customer.getCustomerId());
		return "redirect:cus-All?page=1&pageSize=7";
	}

	/**
	 * 根据id查询销售机会
	 * 
	 * @param salesId
	 * @return
	 */
	@RequestMapping("sales-id")
	@ResponseBody
	public JSONArray selectSaleId(Integer salesId) {
		System.out.println("id:=====================" + salesId);
		// 当前页的内容
		JSONArray list = ss.selectSaleId(salesId);
		return list;
	}

	/**
	 * 添加客户
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("add-cus")
	public String addCus(Customer cus) {

		boolean b =  cs.addCus(cus);
		// 重定向到查询页面
		if(b) {
			return "redirect:cus-All?page=1&pageSize=7";
		}
		return "sdfgf";
	
	}

	/**
	 * 根据id查询
	 * 
	 * @param salesId
	 * @return
	 */
	@RequestMapping("cus-id")
	@ResponseBody
	public JSONArray selectCusId(Integer cusId) {
		// 当前页的内容
		JSONArray list = cs.selectCusId(cusId);
		return list;
	}

	/**
	 * 
	 * 销售机会-客户详情-跟踪日志
	 */
	@RequestMapping("cus-xq")
	@ResponseBody
	public JSONArray selectXq(Integer saleId) {
		JSONArray list = cs.selectXq(saleId);
		return list;

	}

	/**
	 * 
	 * 查销售员
	 */
	@RequestMapping("sale-man")
	@ResponseBody
	public JSONArray selectSaleMan(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ss.selectSaleMan();
	}
	@RequestMapping("login-emp")
	@ResponseBody
	public String selectLoginEmp(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return emp.getEmpName();
	}
	/**
	 * 查询最近添加的销售id
	 * @return
	 */
	@RequestMapping("return-saleId")
	@ResponseBody
	public JSONArray selectMaxId() {
		
		return ss.selectMaxId();
	}
	
	/**
	 * 添加销售机会
	 * 
	 * @param sales
	 * @return
	 */
	@RequestMapping("add-sales")
	public String addSale(SaleVo sales) {
		ss.addSales(sales);
		// 重定向到查询页面
		return "redirect:return-saleId";
	}

	/**
	 * 添加销售日志
	 * @param deliver
	 * @return
	 */
	@RequestMapping("add-saleslog")
	public String addSaleslog(SaleslogVo saleslog) {
		ss.addSalesLog(saleslog);
		return "redirect:sales-All?page=1&pageSize=7";
	}
	/**
	 * 添加销售日志
	 * @param deliver
	 * @return
	 */
	@RequestMapping("add-saleslogs")
	public String addSalesLogs(SaleslogVo saleslog) {
		ss.addSalesLogs(saleslog);
		return "redirect:cus-xq?saleId="+saleslog.getSalesopportunity()+"";
	}
}
